package com.example.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ZhenWuWang
 */
public class TimeUtil
{
    public static LinkedHashMap<Integer, List<Integer>> getYearMonthMapByDateBetween(Date startTime, Date endTime)
    {
        int startYear = getYear(startTime.getTime());
        int endYear = getYear(endTime.getTime());

        int startMonth = getMonth(startTime.getTime());
        int endMonth = getMonth(endTime.getTime());

        if (startYear == endYear && startMonth == endMonth)
        {
            return new LinkedHashMap<>(0);
        }
        LinkedHashMap<Integer, List<Integer>> map = new LinkedHashMap<>(endYear + 1 - startYear);
        for (int year = startYear; year <= endYear; year++)
        {
            List<Integer> months = Stream.iterate(1, n -> n + 1).limit(12).collect(Collectors.toList());
            map.put(year, months);
        }
        //开始年份处理
        List<Integer> startMonths = map.get(startYear);
        startMonths = startMonths.stream().filter(m -> m >= startMonth).collect(Collectors.toList());
        map.put(startYear, startMonths);
        //结束年份处理
        List<Integer> endMonths = map.get(endYear);
        endMonths = endMonths.stream().filter(m -> m <= endMonth).collect(Collectors.toList());
        map.put(endYear, endMonths);
        return map;
    }

    public static int getDay(long timeMillis)
    {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeMillis);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static int getMonth(long timeMillis)
    {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeMillis);
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getYear(long timeMillis)
    {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeMillis);
        return calendar.get(Calendar.YEAR);
    }

    public static SimpleDateFormat getDefaultDateFormat()
    {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    }

    public static Date getDateFromTimeString(String timeString) throws ParseException
    {
        if (timeString == null || timeString.isEmpty())
        {
            return null;
        }
        SimpleDateFormat[] formats = new SimpleDateFormat[]{getDefaultDateFormat()};

        for (SimpleDateFormat format : formats)
        {
            if (isValid(timeString, format))
            {
                return format.parse(timeString);
            }
        }
        return null;
    }

    /**
     * 验证格式是否符合
     */
    public static boolean isValid(String str, SimpleDateFormat format)
    {
        boolean convertSuccess = true;
        try
        {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e)
        {
            convertSuccess = false;
        }
        return convertSuccess;
    }
}
